const pool = require("../db/conn");

exports.mangerLogin = async function (username,password) {
    let sql = "SELECT rc_admin.admin_id id, rc_admin.admin_username username,rc_admin.admin_role_id role_id FROM rc_admin WHERE rc_admin.admin_username = ?  AND rc_admin.admin_password = ? ";
    let conn = await pool.getConnection();
    let [result] = await conn.execute(sql,[username,password]);
    conn.release();
    return result[0];
}

exports.teacherLogin = async function (username,password) {
    let sql = "SELECT rc_teacher.teacher_id id,  rc_teacher.teacher_name username,  rc_teacher.teacher_role_id role_id FROM rc_teacher WHERE rc_teacher.teacher_number = ? AND rc_teacher.teacher_password = ? AND rc_teacher.teacher_is_delete = 0";
    let conn = await pool.getConnection();
    let [result] = await conn.execute(sql,[username,password]);
    conn.release();
    return result[0];
}

exports.studentLogin = async function (username,password) {
    let sql = "SELECT rc_student.student_id id,  rc_student.student_name username, rc_student.student_role_id role_id FROM rc_student WHERE rc_student.student_number = ? AND rc_student.student_password = ?";
    let sql1 = "UPDATE rc_student SET student_last_login_time = now() WHERE student_id = ?"
    let conn = await pool.getConnection();
    let [result] = await conn.execute(sql,[username,password]);
    if(result[0]){
        await conn.execute(sql1,[ result[0]["id"] ]);
    }
    conn.release();
    return result[0];
}